package com.casa_apoio.model.dao;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Query;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.springframework.jdbc.object.SqlQuery;

public abstract class HibernateDAO<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    private Class<T> classe;
    Session session;

    public HibernateDAO(Class<T> classe, Session session) {
        super();
        this.classe = classe;
        this.session = session;
    }

    public void salvar(T entity) {
        session.save(entity);
    }

    public void atualizar(T entity) {
        session.update(entity);
    }

    public void excluir(T entity) {
        session.delete(entity);
    }

    public T getEntity(Integer codigo) {
        T entity = (T) session.get(classe, codigo);
        return entity;
    }

    public List<T> getEntitys() {
        List<T> entitys = session.createCriteria(classe).list();
        return entitys;
    }

    public Criteria criaCriteria() {
        return session.createCriteria(classe);
    }

    public Query criaQuery(String string) {
        return (Query) session.createQuery(string);
    }   
}
